<template>
  <div class="app-container">
    <div class="search-area" v-show="showSearch">
      <el-form
        :model="queryParams"
        @submit.native.prevent
        ref="queryForm"
        size="small"
        :inline="true"
        class="clearfix"
        label-width="auto"
      >
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="拓展类型" prop="expansionTypeList">
          <el-select v-model="queryParams.expansionTypeList" collapse-tags multiple clearable placeholder="请选择拓展类型">
            <el-option-group v-for="group in expansionType" :key="group.label" :label="group.label">
              <el-option v-for="item in group.children" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-option-group>
          </el-select>
        </el-form-item>
        <el-form-item label="统计时段" prop="time">
          <el-date-picker
            style="width: 240px"
            v-model="queryParams.time"
            type="daterange"
            range-separator="至"
            value-format="yyyy-MM-dd"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button type="primary" size="mini" plain @click="handleExport" icon="el-icon-download">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="main-content">
      <el-table v-loading="loading" :data="list">
        <el-table-column label="客户名称" align="center" prop="customerName"> </el-table-column>
        <el-table-column label="市场支出次数" align="center" prop="expendCount">
          <template slot-scope="scope">
            <router-link
              v-if="scope.row.expendCount > 0 && scope.row.customerName != '合计'"
              class="link-type"
              :to="
                '/expansion/list?customerName=' +
                '' +
                scope.row.customerName +
                '&expansionType=' +
                scope.row.expansionType
              "
              >{{ scope.row.expendCount }}</router-link
            >
            <span v-else>{{ scope.row.expendCount || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="拓展类型类型" align="center" prop="expansionTypeStr"> </el-table-column>
        <el-table-column label="市场拓展支出（万元）" align="center" prop="expenses"> </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { marketExpansionDetailReport } from '@/api/biz/index'
import { getExpansionType } from '@/api/biz/expansion'
import dayjs from 'dayjs'
export default {
  name: 'projectSummary',
  // dicts: ['expansion_type_level'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      list: [],
      // 弹出层标题
      title: '',
      // 是否禁止表单填写
      disabled: false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        time: [dayjs().startOf('year').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
        customerName: '',
        expansionType: '',
        expansionTypeList: [],
      },
      expansionId: null,
      showDetails: false,
      typeList: [],
      disableds: false,
      expansionType: [],
    }
  },
  created() {
    this.getExpansionTypes()
    this.getList()
  },
  methods: {
    getExpansionTypes() {
      getExpansionType().then((response) => {
        this.expansionType = response.data
      })
    },
    /** 查询列表 */
    getList() {
      const currentData = this.getParams()
      this.loading = true
      marketExpansionDetailReport(currentData).then((response) => {
        this.list = response.data
        // this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        time: undefined,
        customerName: '',
        expansionType: '',
        expansionTypeList: [],
      }
      this.handleQuery()
    },
    getParams() {
      const currentData = JSON.parse(JSON.stringify(this.queryParams))
      const { time } = currentData
      if (time && time.length > 0) {
        currentData.beginDate = time[0]
        currentData.endDate = time[1]
      }
      delete currentData.time
      return currentData
    },
    /** 导出按钮操作 */
    handleExport() {
      const currentData = this.getParams()
      this.download(
        '/biz/statistics/marketExpansion/marketExpansionDetailReportExport',
        currentData,
        `市场拓展明细报表导出_${new Date().getTime()}.xlsx`
      )
    },
  },
}
</script>
<style scoped lang="scss">
.el-date-editor,
.el-select {
  width: 100%;
}
</style>
